package com.shengyuan.manage_os.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.shengyuan.manage_os.bean.NetURL;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/1/12.
 */

public class DownloadAPK {

    private static final String TAG = "DownloadAPK";
    private static final boolean DEBUG = true;

    private static DownloadAPK mInstance;

    private ProgressDialog downloadProDialog;
    private String appname = "江盛源.apk";

    public DownloadAPK(){}

    public static DownloadAPK getInstance(){
        if(mInstance == null){
            mInstance = new DownloadAPK();
        }
        return mInstance;
    }

    public void download(final Context context){//下载异步线程
        if (DEBUG) Log.i(TAG, "start downloadApkFile");
        new Download(context).execute();
    }

    public class Download extends AsyncTask<Void, Integer, Void> {

        private Context mContext;

        public Download(Context context){
            mContext = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            downloadProgress(mContext);
        }

        @Override
        protected Void doInBackground(Void... params) {
            String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/OA/"+ appname;

            String serverFilePath = NetURL.UPDATE_APK;//这里我模拟下载一个qq老版本APK，相当于从服务器获取
            try {
                URL serverURL = new URL(serverFilePath);
                HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();
                BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());
                File apkfile = new File(savePath);
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(apkfile));

                int n;
                byte[] buffer = new byte[1024];
                while((n=bis.read(buffer, 0, buffer.length))!=-1){
                    bos.write(buffer, 0, n);
                }
                bis.close();
                bos.close();
                connect.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                if (DEBUG) Log.e(TAG, "downloadFile exception !" + e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            downloadProDialog.dismiss();
            install(mContext);
        }
    }

    public void downloadProgress(Context context){//自己定义的一个下载的进度条
        downloadProDialog = new ProgressDialog(context);
        downloadProDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        downloadProDialog.setTitle("正在下载");
        downloadProDialog.setMessage("下载中,请等待...");
        downloadProDialog.setMax(100);
        downloadProDialog.setProgress(0);
        downloadProDialog.setSecondaryProgress(0);
        downloadProDialog.setIndeterminate(false);
        downloadProDialog.setCancelable(false);
        downloadProDialog.show();
    }

    private void install(Context context){
        String fileName = Environment.getExternalStorageDirectory() +"/OA/"+ appname;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}
